// 默认缩放值
const scale = {
  width: "1",
  height: "1",
};

// 设计稿尺寸
const baseWidth = 1920;
const baseHeight = 1080;

// 需要保持的比例
const baseProportion = parseFloat(baseWidth / baseHeight).toFixed(5);

// 定时函数
let drawTiming = null;

window.onload = function () {
  resize();
  window.addEventListener("resize", function () {
    resize();
  });
};

function resize() {
  clearTimeout(drawTiming);
  drawTiming = setTimeout(() => {
    calcRate();
  }, 200);
}

function calcRate() {
  const appEl = document.getElementById("index");
  if (!appEl) return;

  // 当前宽高比
  const currentRate = parseFloat(window.innerWidth / window.innerHeight).toFixed(5);
  if (appEl) {
    if (currentRate > baseProportion) {
      // 表示更宽
      scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5);
      scale.height = (window.innerHeight / baseHeight).toFixed(5);
      appEl.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
    } else {
      // 表示更高
      scale.height = (window.innerWidth / baseProportion / baseHeight).toFixed(5);
      scale.width = (window.innerWidth / baseWidth).toFixed(5);
      appEl.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
    }
  }
}
